import router from '@system.router';
import CourseApi from '../../api/courseApi.js'
import prompt from '@system.prompt';

export default {
    data: {
        baseImageUrl: '',
        tab: 0,
        isOver: false,
        pageNum: 1,
        total: 0,
        courses: [],
        courses_time: [],
        courses_hot: [],
//        courses_test: [],
//        tlist: [
//            {
//                "id": 0, "classHour": 48
//            },
//            {
//                "id": 1, "classHour": 64
//            },
//            {
//                "id": 2, "classHour": 40
//            },
//            {
//                "id": 3, "classHour": 24
//            },
//            {
//                "id": 4, "classHour": 120
//            },
//        ],
//        txt: '1111'
    },
    onInit() {
        this.getCourses();
//        this.getCourses_Time();
        this.baseImageUrl = this.$app.$def.globalData.baseImageUrl;
    },
    goBack() {
        router.back();
    },
    changeTabs(index) {
        this.tab = index;
    },
    getCourses_Time() {
        this.courses_time = this.quickSort(this.courses_time);
    },
    getCourses() {
        CourseApi.getCourses(this.pageNum, (data) => {
            this.courses = this.courses.concat(data.rows);
            this.courses_hot = this.courses_hot.concat(data.rows);
            this.courses_time = this.courses_time.concat(data.rows);
            this.total = data.total;
            let pageTotal = Math.ceil(data.total / 5)
            this.isOver = this.pageNum == pageTotal;
        });

    },
    viewDetail(id) {
        router.push({
            uri: 'pages/courseDetail/courseDetail',
            params: {
                id
            }
        });
    },
    scroll() {
        if (!this.isOver) {
            this.pageNum++;
            this.getCourses();
            this.getCourses_Time();
        }
    },

    quickSort(arr) {
        if (arr.length <= 1) {
            return arr // 递归的出口
        }

        let flag = arr[0]; // 拿到数组的第一个元素作为flag
        let left = [];
        let right = [];
        let non = [];
        for (let i = 1;i < arr.length; i++) {
            if (parseInt(arr[i].classHour) < parseInt(flag.classHour)) {
                left.push(arr[i])
            } else if (parseInt(arr[i].classHour) > parseInt(flag.classHour)){
                right.push(arr[i])
            }else{
                non.push(arr[i])
            }
        }
        return ((this.quickSort(right).concat(flag)).concat(this.quickSort(left))).concat(non) // 递归
    },


//        test(){
//            let temp = this.courses;
//            this.courses_time = this.quickSort(temp);
//            this.courses_test = this.quickSort(this.tlist);
//            this.txt=this.courses_time[0].classHour+','+this.courses_time[1].classHour+','+this.courses_time[2].classHour+','+this.courses_time[3].classHour+','+this.courses_time[4].classHour;
//            this.scroll();
//        },

    changeFruit(e) {
        if (e.newValue == 'byTime') {
            this.getCourses_Time();
            this.courses = this.courses_time;

        } else if (e.newValue == 'byHot') {
            this.courses = this.courses_hot;
        }
    }
}
